#include <STDIO.H>
#include <STDLIB.H>
 
#define MAX_EXP 100			//表达式最大长度
#define MAX_INT 10			//整数最大位数
#define NO_RESULT -99999	//计算异常的返回值
 
enum node_type{ NUM, OP };
 
struct node{
	int number;
	char operation;
	enum node_type type;
	struct node *next;
};
 
struct stack{
	struct node *top;
	int length;
};
 
typedef struct node Node;
typedef struct stack Stack;
 
int GetResult(const char []);
void StringToMidExp(const char [], Stack *);
int TenPow(int);
void MidExpToBackExp(Stack *, Stack *);
int BackExpToResult(Stack *);
void ShowStack(const Stack *);
void ShowNode(const Node *);
void InitStack(Stack *);
void Push(Stack *, Node *);
void Pop(Stack *, Node *);
void ClearStack(Stack *);
void Reverse(Stack *, Stack *);
 
int main(void)
{
	char expression[MAX_EXP];
	int result;
 
	printf("输入四则运算表达式：\n");
	scanf("%s", expression);
 
	result = GetResult(expression);
	if (result == NO_RESULT)
	{
		printf("表达式有误，计算失败。\n");
	} 
	else
	{
		printf("计算结果是：%d\n", result);
	}
 
	return 0;
}
 
//根据表达式的字符串计算结果
int GetResult(const char exp[])
{
	Stack middleExp, backExp;
	Stack *pm, *pb;
 
	pm = &middleExp;
	pb = &backExp;
	InitStack(pm);
	InitStack(pb);
	StringToMidExp(exp, pm);
	printf("中缀表达式：");
	ShowStack(pm);
	MidExpToBackExp(pm, pb);
	printf("后缀表达式：");
	ShowStack(pb);
 
	return BackExpToResult(pb);
}
 
//字符串转换成中缀表达式
void StringToMidExp(const char exp[], Stack *ps)
{
	int num[MAX_INT];
	int k, n, count, temp;
	Stack m_stack;
	Stack *pm;
	Node *q;
 
	k = 0;
	count = 0;
	pm = &m_stack;
	InitStack(pm);
 
	while (exp[k] != '\0')
	{
		if (exp[k] >= '0' && exp[k] <= '9') //数字0到9
		{
			count++;					//count记录整数的位数
			num[count-1] = exp[k] - 48;	//num数组记录整数的每一位
		}
		else if ((exp[k] >= 40 && exp[k] <= 43) || exp[k] == 45 || exp[k] == 47) //运算符
		{
			if (count > 0) //转换该运算符之前的数字
			{
				n = 0;
				temp = 0;
				while (n < count)
				{
					temp += num[n] * TenPow(count - n -1); //每一位乘以10的某次方
					n++;
				}				
				q = (Node *)malloc(sizeof(Node));
				q->type = NUM;
				q->number = temp;
				Push(pm, q);
			}
			count = 0; //位数清零
			q = (Node *)malloc(sizeof(Node));
			q->type = OP;
			q->operation = exp[k];
			Push(pm, q);
		}
		k++;
	}
	if (count > 0) //把最后一个数字转换出来
	{
		n = 0;
		temp = 0;
		while (n < count)
		{
			temp += num[n] * TenPow(count - n -1);
			n++;
		}
		q = (Node *)malloc(sizeof(Node));
		q->type = NUM;
		q->number = temp;
		Push(pm, q);
	}
	Reverse(pm, ps); //颠倒一下次序
}
 
//计算10的n次方
int TenPow(int n)
{
	if (n == 0)
	{
		return 1;
	} 
	else
	{
		int i, k;
		i = 0;
		k = 1;
		while (i < n)
		{
			k *= 10;
			i++;
		}
		return k;
	}
}
 
//中缀表达式转换成后缀表达式
void MidExpToBackExp(Stack *pm, Stack *pb)
{
	Stack tempStack, oprStack;
	Stack *pt, *pr;
	Node *q, *r;
 
	pt = &tempStack;	//临时存储后缀表达式
	pr = &oprStack;		//用来决定运算符的顺序
	InitStack(pt);
	InitStack(pr);
 
	while (pm->top)
	{
		q = (Node *)malloc(sizeof(Node));
		Pop(pm, q);
		if (q->type == NUM)
		{
			Push(pt, q);
		} 
		else
		{
			if (q->operation == '+' || q->operation == '-')
			{
				while (pr->top && pr->top->operation != '(')
				{
					r = (Node *)malloc(sizeof(Node));
					Pop(pr, r);
					Push(pt, r);
				}
				Push(pr, q);
			}
			else if (q->operation == '*' || q->operation == '/')
			{
				while (pr->top && pr->top->operation != '(' && pr->top->operation != '+' && pr->top->operation != '-')
				{
					r = (Node *)malloc(sizeof(Node));
					Pop(pr, r);
					Push(pt, r);
				}
				Push(pr, q);
			}
			else if (q->operation == '(')
			{
				Push(pr, q);
			} 
			else
			{
				while (pr->top)
				{
					if (pr->top->operation == '(')
					{
						r = (Node *)malloc(sizeof(Node));
						Pop(pr, r);
						free(r);
						break;
					}
					r = (Node *)malloc(sizeof(Node));
					Pop(pr, r);
					Push(pt, r);
				}
				free(q);
			}
		}
	}
	while (pr->top) //栈内剩余运算符全部出栈
	{
		r = (Node *)malloc(sizeof(Node));
		Pop(pr, r);
		Push(pt, r);
	}
	Reverse(pt, pb); //颠倒一下次序
}
 
//根据后缀表达式计算结果
int BackExpToResult(Stack *ps)
{
	if (!ps->top) //空栈说明表达式有误
	{
		return NO_RESULT;
	}
 
	Stack tempStack;
	Stack *pt;
	Node *q;
	int num_left, num_right, result;
 
	pt = &tempStack;
	InitStack(pt);
	while (ps->top)
	{
		if (ps->top->type == NUM)
		{
			q = (Node *)malloc(sizeof(Node));
			Pop(ps, q);
			Push(pt, q);
		} 
		else
		{
			q = (Node *)malloc(sizeof(Node));
			Pop(pt, q);
			num_right = q->number;
			free(q);
 
			if (!pt->top) //pt栈内没有第2个数了，说明表达式有误
			{
				return NO_RESULT;
			}
 
			q = (Node *)malloc(sizeof(Node));
			Pop(pt, q);
			num_left = q->number;
			free(q);
 
			q = (Node *)malloc(sizeof(Node));
			Pop(ps, q);
			switch(q->operation)
			{
			case '+':
				result = num_left + num_right;
				break;
			case '-':
				result = num_left - num_right;
				break;
			case '*':
				result = num_left * num_right;
				break;
			case '/':
				result = num_left / num_right;
				break;				
			}
			free(q);
 
			q = (Node *)malloc(sizeof(Node));
			q->type = NUM;
			q->number = result;
			Push(pt, q);
		}
	}
	q = (Node *)malloc(sizeof(Node));
	Pop(pt, q);
	result = q->number;
	free(q);
 
	if (pt->top) //pt栈内还有数字，说明表达式有误
	{
		return NO_RESULT;
	} 
	else
	{
		return result;
	}
}
 
//显示栈中元素
void ShowStack(const Stack *ps)
{
	if (ps->top)
	{
		Node *p = ps->top;
		while (p->next)
		{
			ShowNode(p);
			printf(" ");
			p = p->next;
		}
		ShowNode(p);
		printf("\n");
	} 
	else
	{
		printf("无\n");
	}
}
 
//显示一个节点元素
void ShowNode(const Node *p)
{
	if (p->type == NUM)
	{
		printf("%d", p->number);
	} 
	else
	{
		printf("%c", p->operation);
	}
}
 
//初始化栈
void InitStack(Stack *ps)
{
	ps->length = 0;
	ps->top = NULL;
}
 
//节点入栈
void Push(Stack *ps, Node *pn)
{
	pn->next = ps->top;
	ps->top = pn;
	ps->length++;
}
 
//节点出栈
void Pop(Stack *ps, Node *pn)
{
	if (ps->top)
	{
		Node *q = ps->top;
		pn->next = NULL;
		pn->number = q->number;
		pn->operation = q->operation;
		pn->type = q->type;
		ps->top = q->next;
		free(q);
		ps->length--;
	} 
	else
	{
		pn = NULL;
	}
}
 
//清空栈
void ClearStack(Stack *ps)
{
	Node *q;
 
	while (ps->top)
	{
		q = ps->top;
		ps->top = q->next;
		free(q);
		ps->length--;
	}
}
 
//反转栈中元素的次序
void Reverse(Stack *ps1, Stack *ps2)
{
	if (ps1->top)
	{
		Node *q;
 
		ClearStack(ps2);
		while (ps1->top)
		{
			q = (Node *)malloc(sizeof(Node));
			Pop(ps1, q);
			Push(ps2, q);
		}
	} 
	else
	{
		ps2->top = NULL;
		ps2->length = 0;
	}
}